Semaphore Class

Used to control access to resources in a multithreaded environment.

Events

None

Properties

None

Methods

Release

Signal

TrySignal


More information available in parent classes: Object


Constructor

The Semaphore has a constructor that takes the initial count of the number of resources the semaphore is protecting as an optional parameter.

NameParametersDescription
Semaphore NumResources as Integer The number of resources the Semaphore is intended to protect. This defaults to 1. If you need to manage access to a single resource, you can instead use a CriticalSection.

Every time you successfully obtain a lock on the resource, the Semaphore will decrement its internal count of available resources. When there are no more, threads that request locks will begin to block and wait for resources. This is why you are allowed to pass the initial count of resources, to give you more control over the behavior of the Semaphore.

The Semaphore class is different from the CriticalSection and Mutex classes in this way: calling Signal in the same thread will cause the counter to decrement. If you call Signal recursively, you will cause the application to hang.


Notes

A Semaphore is an object that can be used to coordinate access to a shared resource.

To acquire the ownership of a semaphore, a thread calls the Signal or TrySignal methods. If the Semaphore isn't owned, the thread acquires ownership, otherwise the thread will be forced to wait until the Semaphore is released via the Release method by the owning thread.


See Also

CriticalSection, Mutex, Thread classes.